<?php

namespace app\admin\logic;

use nx\Data;
use think\Cache;
use app\admin\model\Classify as Classify_model;
use think\Db;

/**
 * Class Classify 类别操作数据
 * @package app\admin\logic
 * @author: Huang
 */
class Classify
{
    public $Classify_model = '';

    /**获取模型
     * @return Classify_model|string
     */
    public function getModel()
    {
        if (empty($this->Classify_model)) {
            $this->Classify_model = new Classify_model();
        }
        return $this->Classify_model;
    }

    /**获取所有的类别类别（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function ClassifysToText()
    {
        $Classifys = self::getAllClassifys();
        $Classifys = collect($Classifys)->column('name', 'id');
        return $Classifys;
    }

    /**将所有类别进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllClassifyToTree()
    {
        $Classifys = $this->getAllClassifys();
        $Classifys = Data::tree($Classifys, 'title', 'id', 'pid');
        return $Classifys;
    }

    /**获得所有的类别类别（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllClassifys()
    {
        $Classifys = Cache::get('allClassifys');
        if (!empty($Classifys)) return $Classifys;
        $Classifys = $this->getModel()->order('sort')->select();
        if (empty($Classifys)) return $Classifys;
        $Classifys = collection($Classifys)->append(['status_text'])->toArray();
        Cache::set('allClassifys', $Classifys);
        return $Classifys;
    }


    /**
     * 获取类别 select 数据
     * @return array
     * @author: Huang
     */
    public function getAllText($pid)
    {
        $Classifys = $this->getAllClassifys();
        $companys = collect($Classifys)->where('pid',$pid)->column('title', 'id');
        return $companys;
    }

    /**
     * 获取某类别的所有下属类别
     * @param $Classify_id
     * @return array|mixed
     */
    public function getNextClassifys($Classify_id)
    {
        $data = $this->getAllClassifys();
        $Classifys = Data::channelList($data, $Classify_id, '— ', 'id', 'pid');
        return $Classifys;
    }


    public function addClassify($data)
    {
        if (empty($data)) {
            return ['result' => false, 'msg' => '数据丢失'];
        }
        if (!empty($data['code'])) {
            $data['code'] = strtoupper(trim($data['code']));
        }
        $result = $this->getModel()->allowField(true)->validate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allClassifys');
            return ['result' => true, 'msg' => '类别添加成功'];
        }
    }

    public function editClassify($data)
    {
        if (empty($data['id'])) {
            return ['result' => false, 'msg' => 'ID丢失'];
        }
        isset($data['status']) ?: $data['status'] = 1;
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allClassifys');
            return ['result' => true, 'msg' => '类别修改成功'];
        }
    }

    public function delClassify($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Classify_model::get($id)) {
            if (Classify_model::destroy($id)) {
                Cache::rm('allClassifys');
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }

    /**获取报名状态（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function SignupTypeToInt(){
        $signuptype = Db::name('classify')->where(['pid'=>62])->cache(600)->column('id','title');
        return $signuptype;
    }

}
